import {hashHistory,router } from 'react-router';
import _ from "underscore";
import NativePage from "./NativePage";
import SApi from "./ServiceApi"; 

/**
  路由工具类
*/
var RouterHandle = {
	routeHashs:[],
	rcMaps:{},
	regComponent:function(cfg){ 
		this.rcMaps[cfg.path]=cfg;
		this.routeHashs.push(cfg.path); 
	},
    regCmp:function(cfg){
        this.regComponent(cfg);
    },
    //前往某个路由
    go:function(path,params){ 
        var me=this;
        //如果参数中发现是启动监听的则执行监听
    	if(params&&params.listenerAble){

            this._invokeListener(path,params,function(){me._go(path,params)})
        }
        else
        {
            this._go(path,params);
        }
    },
    _invokeListener:function(path,params,next){
        var isEnable=params.listenerAble.beforeGo;
        var cmp=this.rcMaps[path];
        if(isEnable&&cmp&&cmp.listeners.beforeGo){
            cmp.listeners.beforeGo(next);
        }
        else
        {
            next();  
        }
    },
    //处理URL得以正确存储
    pathProcess:function(path){
        if(path.indexOf("/login/")>-1)
        {
           path=path.substring(path.lastIndexOf("/")+1,path.length);
           path=path.replaceAll("_","/");
        }
        else
        {
          this.routeHashs.push(path);  
        } 
    },
    _go:function(path,params){
        window.location.type="forward";
        var me=this;
        var nativePath=NativePage.match(path);
        console.log("nativePath",nativePath);
        if(nativePath){
            SApi.goPage({
                params:{
                    url:nativePath,
                    commandHash:new Date().getTime(),
                    params:params
                },
                success:function(rs){
                    me.pathProcess(path);
                    if(callback){
                        callback(rs);
                    }
                },
                error:function(rs){
                    me.pathProcess(path);
                    hashHistory.push(path);
                }
            });
        }
        else
        { 
            me.pathProcess(path);
            hashHistory.push(path);
        }
    },
    //返回如果path为空，则使用hashhistory的back
    _back:function(path,nonAnimate,animateName){ 
    	window.location.type="back";
        console.log("----back----");
        if(path){
            this.back(path);
        }
        else
        {
            var lastPath=this.routeHashs.pop();
            //如果能正常读取最后一个path则证明不是root路由，否则就是root，如果此时是native环境，则调用native的返回键
            if(lastPath){
                if(this.rcMaps[lastPath]){
                    var cpm=this.rcMaps[lastPath].component; 
                    if(cpm&&cpm.back){
                        cpm.back();
                    }
                    else
                    {
                        this.back();
                    }
                }else{
                    var prev=this.routeHashs[this.routeHashs.length-1];
                    if(prev){
                        this.back(prev);
                    }
                    else
                    {
                        this.back();
                    }
                    
                }
            }
            else
            {
                if(window.cordova)
                {
                    SApi.nativeBack();
                }
                else
                {
                   this.routeHashs=[];
                   this.back("/");
                }
                
            } 
        }
    	
    	
    },
    back:function(path){
    	window.location.type="back";
    	if(path){
    		hashHistory.push(path);
    	}
    	else
    	{
    		hashHistory.goBack();
    	}
    },
    clearHashs:function(){
        this.routeHashs=[];
    }
};
module.exports = RouterHandle;





